class CircularQueue {
    private int[] elements;
    private int front;
    private int rear;
    private int size;

    public CircularQueue(int capacity) {
        elements = new int[capacity];
        front = 0;
        rear = -1;
        size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean isFull() {
        return size == elements.length;
    }

    public void enqueue(int item) {
        if (isFull()) {
            throw new RuntimeException("Queue is full");
        }
        rear = (rear + 1) % elements.length;
        elements[rear] = item;
        size++;
    }

    public int dequeue() {
        if (isEmpty()) {
            throw new RuntimeException("Queue is empty");
        }
        int item = elements[front];
        front = (front + 1) % elements.length;
        size--;
        return item;
    }
}

class YangHuiTriangleWithQueue {
    public static void main(String[] args) {
        int n = 6;
        CircularQueue queue = new CircularQueue(n * n);
        queue.enqueue(1);

        for (int i = 0; i < n; i++) {
            int[] row = new int[i + 1];
            row[0] = 1;
            for (int j = 1; j < i; j++) {
                int prev1 = queue.dequeue();
                int prev2 = queue.isEmpty()? prev1 : queue.dequeue();
                int current = prev1 + prev2;
                queue.enqueue(current);
                row[j] = current;
            }
            row[i] = 1;
            queue.enqueue(1);

            for (int num : row) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}